#include "editbookwindow.h"

EditBookWindow::EditBookWindow(const QString &bookId, Database *db, QWidget *parent)
    : QDialog(parent)
    , bookId(bookId)
    , db(db)
{
    setupUI();
    createConnections();
    loadBookInfo();
}

EditBookWindow::~EditBookWindow()
{
}

void EditBookWindow::setupUI()
{
    setWindowTitle("修改图书信息");
    setFixedSize(400, 300);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(20);
    mainLayout->setContentsMargins(20, 20, 20, 20);

    // 书名
    QLabel *titleLabel = new QLabel("书名:", this);
    titleEdit = new QLineEdit(this);
    titleEdit->setPlaceholderText("请输入书名");
    titleEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(titleEdit);

    // 作者
    QLabel *authorLabel = new QLabel("作者:", this);
    authorEdit = new QLineEdit(this);
    authorEdit->setPlaceholderText("请输入作者");
    authorEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(authorLabel);
    mainLayout->addWidget(authorEdit);

    // 出版社
    QLabel *publisherLabel = new QLabel("出版社:", this);
    publisherEdit = new QLineEdit(this);
    publisherEdit->setPlaceholderText("请输入出版社");
    publisherEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(publisherLabel);
    mainLayout->addWidget(publisherEdit);

    // 可借阅状态
    availableCheckBox = new QCheckBox("可借阅", this);
    mainLayout->addWidget(availableCheckBox);

    // 按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(20);

    editButton = new QPushButton("修改", this);
    cancelButton = new QPushButton("取消", this);

    editButton->setStyleSheet("QPushButton { background-color: #FFC107; color: black; padding: 8px 16px; "
                            "border: none; border-radius: 4px; }"
                            "QPushButton:hover { background-color: #FFA000; }"
                            "QPushButton:pressed { background-color: #FF6F00; }");

    cancelButton->setStyleSheet("QPushButton { background-color: #F44336; color: white; padding: 8px 16px; "
                              "border: none; border-radius: 4px; }"
                              "QPushButton:hover { background-color: #D32F2F; }"
                              "QPushButton:pressed { background-color: #B71C1C; }");

    buttonLayout->addWidget(editButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);

    // 设置窗口样式
    setStyleSheet("QDialog { background-color: white; }");
}

void EditBookWindow::createConnections()
{
    connect(editButton, &QPushButton::clicked, this, &EditBookWindow::onEditClicked);
    connect(cancelButton, &QPushButton::clicked, this, &EditBookWindow::onCancelClicked);
}

void EditBookWindow::loadBookInfo()
{
    QSqlQuery query = db->getBookInfo(bookId);
    if (query.next()) {
        titleEdit->setText(query.value("title").toString());
        authorEdit->setText(query.value("author").toString());
        publisherEdit->setText(query.value("publisher").toString());
        availableCheckBox->setChecked(query.value("available").toBool());
    }
}

void EditBookWindow::onEditClicked()
{
    QString title = titleEdit->text().trimmed();
    QString author = authorEdit->text().trimmed();
    QString publisher = publisherEdit->text().trimmed();
    bool available = availableCheckBox->isChecked();

    if (title.isEmpty() || author.isEmpty() || publisher.isEmpty()) {
        QMessageBox::warning(this, "警告", "所有字段都不能为空！");
        return;
    }

    if (db->updateBook(bookId, title, author, publisher, available)) {
        QMessageBox::information(this, "成功", "图书信息修改成功！");
        accept();
    } else {
        QMessageBox::warning(this, "错误", "图书信息修改失败！");
    }
}

void EditBookWindow::onCancelClicked()
{
    reject();
} 